// pages/phone/phone.js
const app = getApp()
var server = require('../../utils/server');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    yzm: '获取',
    disab: false,
    yzmstatus: 1,
    times: 1,
    tupian: app.globalData.tupian,
    phone: '',
    yyzm: '',
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    var that = this
  },
  phone: function (e) {
    var phone = e.detail.value
    this.setData({
      phone: phone
    })
  },
  yyzm: function (e) {
    var yyzm = e.detail.value
    this.setData({
      yyzm: yyzm
    })
  },
  yzm: function (e) {
    var that = this
    // that.sshow()
    // 获取网络状态 
    wx.getNetworkType({
      success: function (res) {
        console.log('load网络连接状态', res.networkType)
        if (res.networkType != 'none') {
          if (that.data.phone.length == 11) {
            server.requestForm(false, 'post', 'user/authCode', {
              mobile: that.data.phone,
            }, function (e) {
              console.log('yanzhengma', e)
              if (e.code == 0) {
                that.sshow()
              } else {
                wx.showToast({
                  title: e.mesg,
                  icon: "none",
                  duration: 2000
                })
              }

            })
          } else {
            wx.showToast({
              title: '请输入正确的手机号',
              icon: "none",
              duration: 2000
            })
          }
        } else {
          wx.showToast({
            title: '无网络连接',
            icon: "none",
            duration: 2000
          })
        }
      }
    })
  },
  sshow: function () {
    this.setData({
      disab: true,
      yzm: "60秒",
      yzmstatus: 2,
    })
    var timer = this.data.times
    if (timer == 1) {
      timer = 0
      var that = this
      var time = 59
      var inter = setInterval(function () { //循环执行代码  setInterval
        that.setData({
          yzm: time + "秒"
        })
        time--
        if (time < 0) {
          timer = 1
          clearInterval(inter)
          that.setData({
            disab: false,
            yzm: "获取",
            yzmstatus: 1,
          })
        }
      }, 1000)
    }
  },
  tj: function (e) {
    var that = this
    server.requestForm(false, 'put', 'user/authCodeValidation', {
      mobile: that.data.phone,
      authCode: that.data.yyzm,
      userID: app.globalData.userID
    }, function (e) {
      console.log('yanzhengma', e)
      if (e.code == 0) {
        wx.showToast({
          title: '成功',
          icon: "none",
          duration: 2000
        })
        wx.reLaunch({
          url: '../mine/my/my',
        })
      } else {
        wx.showToast({
          title: e.mesg,
          icon: "none",
          duration: 2000
        })
      }
    })
  }

})